﻿using DbMapper.controller;
using DbMapper.model;
using DbMapper.service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DbMapper.view
{
    public partial class LoginWindow : Form
    { 
        DbInfo dbInfo = new DbInfo();
        public  string Host { get { return this.ServerComboBox.Text; } }
        public string User { get { return this.UserNameTXTBox.Text; } }
        public string Password { get { return this.PasswordTXTBox.Text; } }
        public string Database { get { return this.DatebaseTextBox.Text; } }
        public string strConnSQL { get { return  string.Format("Data Source = {0}; Initial Catalog = {1}; User ID = {2}; Password={3};", Host, Database, User, Password); } }
        public LoginWindow()
        {
            InitializeComponent();
            this.DatabaseTypeComboBox.SelectedIndex=0;
        }
        private bool CheckServer()
        {
            if (Host != "" && User != ""  && Database != "")
            {
                return DbMapperService.ValidateDatabase(new DbInfo(Host, User, Password, Database));
            } else
            {
                return false;
            }
        }
        private void OkButton_Click(object sender, EventArgs e)
        {
            if (CheckServer())
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            } else
            {
                MessageBox.Show("登录失败，请重新登陆", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
                this.ServerComboBox.Text  = "";
                this.UserNameTXTBox.Text = "";
                this.PasswordTXTBox.Text = "";
                this.DatebaseTextBox.Text = "";
            }
        }
        private void CancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
